package com.app.shop.util;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.io.Serializable;
import java.util.Collection;
import java.util.Date;


/**
 * 用户实体类，实现Spring Secunity UserDetails接口
 *
 * @author chenliwei
 * @version v1.0 2017-11-5
 */

public class UserEntity implements Serializable, UserDetails {
    private static final long serialVersionUId = 1L;

    public UserEntity() {
    }

    public UserEntity(Integer userId, String username, String name, String phone, String email, Integer status) {
        this.userId=userId;
        this.username=username;
        this.name=name;
        this.phone=phone;
        this.email=email;
        this.status=status;
    }


    private  Boolean isRememberMe;

    private Integer userId;

    private String username;

    private String password;

    private String name;

    private Integer orgId;

    private String phone;

    private String email;

    private Integer status;

    private Date lastLoginTime;

    private Date createTime;

    private Integer createUserId;

    private int onlineStatus;

    private String lastIp;

    private String lastAddress;

    private String sex;

    private String role;



    /**
     * 指示用户的帐户是否已过期过期帐户不能认证
     *
     * @return 如果用户帐户有效则返回 true
     * 如果没有有效（即过期）false
     * @author chenliwei
     */
    @Override
    public boolean isAccountNonExpired() {
        if (getStatus() == 2 && getStatus()!=null) {
            return false;
        } else {
            return true;
        }

    }


    /**
     * 指示用户是否已锁定或未锁定, 锁定的用户不能认证
     *
     * @return 如果用户未锁定 true
     * 如果用户锁定false
     * @author chenliwei
     */
    @Override
    public boolean isAccountNonLocked() {
        if (getStatus() == 3 && getStatus()!=null) {
            return false;
        } else {
            return true;
        }
    }


    /**
     * 指示用户的凭据（密码）是否已过期。期满凭证防止身份验证
     *
     * @return 如果用户凭证有效（非过期） true
     * 如果没有有效（即过期）false
     * @author chenliwei
     */
    @Override
    public boolean isCredentialsNonExpired() {
        if (getStatus() == 4 && getStatus()!=null) {
            return false;
        } else {
            return true;
        }
    }

    /**
     * 指示用户是否已启用或禁用。禁用的用户不能认证
     *
     * @return 如果用户已启用 true
     * 如果用户已禁用false
     * @author chenliwei
     */
    @Override
    public boolean isEnabled() {
        if (getStatus() == 5 && getStatus()!=null) {
            return false;
        } else {
            return true;
        }
    }


    /**
     * 返回授予用户的权限，不能返回 null
     *
     * @return 返回权限，按自然键排序
     * @author chenliwei
     */
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
//        List<GrantedAuthority> auths = new ArrayList();
//        String roleName = "ROLE_SYSTEM_LOGIN";
//        auths.add(new SimpleGrantedAuthority(roleName));
//        auths.add(new SimpleGrantedAuthority(roleName));
//        auths.add(new SimpleGrantedAuthority(roleName));
//        auths.add(new SimpleGrantedAuthority(roleName));
//        auths.add(new SimpleGrantedAuthority(roleName));
//        auths.add(new SimpleGrantedAuthority(roleName));
        return null;
    }


    public Integer getUserId() {
        return userId;
    }

    public void setUserId(Integer userId) {
        this.userId = userId;
    }

    @Override
    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getOrgId() {
        return orgId;
    }

    public void setOrgId(Integer orgId) {
        this.orgId = orgId;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public Date getLastLoginTime() {
        return lastLoginTime;
    }

    public void setLastLoginTime(Date lastLoginTime) {
        this.lastLoginTime = lastLoginTime;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public Integer getCreateUserId() {
        return createUserId;
    }

    public void setCreateUserId(Integer createUserId) {
        this.createUserId = createUserId;
    }


    public Integer getOnlineStatus() {
        return onlineStatus;
    }

    public void setOnlineStatus(Integer onlineStatus) {
        this.onlineStatus = onlineStatus;
    }

    public String getLastIp() {
        return lastIp;
    }

    public void setLastIp(String lastIp) {
        this.lastIp = lastIp;
    }

    public String getLastAddress() {
        return lastAddress;
    }

    public void setLastAddress(String lastAddress) {
        this.lastAddress = lastAddress;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getRole() {
        return role;
    }

    public void setRole(String role) {
        this.role = role;
    }

    public Boolean getRememberMe() {
        return isRememberMe;
    }

    public void setRememberMe(Boolean rememberMe) {
        isRememberMe = rememberMe;
    }
}
